package stack;

import java.util.Stack;

/**
 * Created by admin on 17/3/28.
 */
public class TestStack {
    private static void ten2n(int m, int n) {
        Stack<Integer> stack = new Stack<>();
        //除后不为0的依次入栈
        while (m / n != 0) {
            stack.push(m % n);
            m = m / n;
        }
        //入栈最后一位
        stack.push(m % n);

        StringBuffer sb = new StringBuffer();
        //栈里有值，就依次出栈
        while (!stack.empty()) {
            Integer x = stack.pop();
            sb.append(x);
        }
        System.out.print(sb.toString());
    }

    private static boolean match(String s) {
        Stack<Character> stack = new Stack<>();
        if (s == null) {
            throw new NullPointerException("空串");
        }
        //遍历s的每个字符，如果为左括号就入栈，如果为右括号，就和栈顶的进行匹配，如果不匹配，就return false，如果匹配，就移除栈顶的左括号
        for (int i = 0; i < s.length(); i++) {
            char c = s.charAt(i);
            if (c == '(' || c == '[' || c == '{') {
                stack.push(c);
            } else {
                //如果出现的是右括号，但是栈里已经空了，说明肯定不匹配
                if (stack.empty()) {
                    return false;
                }
                if (c == ')') {
                    //和栈顶的匹配，继续下次循环。不匹配，return false
                    if ('(' != stack.pop()) {
                        return false;
                    }
                }
                if (c == ']') {
                    //和栈顶的匹配，继续下次循环。不匹配，return false
                    if ('[' != stack.pop()) {
                        return false;
                    }
                }
                if (c == '}') {
                    //和栈顶的匹配，继续下次循环。不匹配，return false
                    if ('{' != stack.pop()) {
                        return false;
                    }
                }
            }
        }
        //循环完毕，如果栈空，则匹配完毕。不空则不匹配
        return stack.empty();
    }

    public static void main(String[] args) {
        ten2n(16, 8);
        System.out.println(match("((()[[]])"));
        int i = 1;
        String s = "ab1c";
        String s1 = "ab" + i + "c";
        System.out.println(s == s1);

        String a = "35236475874587686585333456";
        int index = a.indexOf("768");
        System.out.println(index);
    }
}
